package com.golddaymicromall.activity.tickert;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.alibaba.fastjson.JSON;
import com.golddaymicromall.activity.R;
import com.golddaymicromall.base.BaseActivity;
import com.golddaymicromall.bean.TicketBindStateInfo;
import com.golddaymicromall.util.ActivityController;
import com.golddaymicromall.util.SPUtils;
import com.nispok.snackbar.Snackbar;
import com.nispok.snackbar.SnackbarManager;

import java.util.concurrent.TimeUnit;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;

import static com.google.common.base.Preconditions.checkNotNull;

public class TicketActivity extends BaseActivity implements TicketContract.View {

    TicketContract.Presenter mPresenter;
    @Bind(R.id.ticket_back)
    LinearLayout ticketBack;
    @Bind(R.id.ticket_ticketNo)
    EditText ticketNo;
    @Bind(R.id.ticket_bind)
    Button ticketBind;
    @Bind(R.id.ticket_check)
    Button ticketCheck;

    String id;
    String ticketno;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ticket);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        new TicketPresenter(this);
        String ticketFlag = SPUtils.get(getApplicationContext(), "ticketFlag", "").toString();
        if (!"".equals(ticketFlag)) {
            ticketNo.setText(ticketFlag);
        }
    }

    @OnClick({R.id.ticket_back, R.id.ticket_bind, R.id.ticket_check})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ticket_back:
                finish();
                ActivityController.getInstance().removeActivity(this);
                break;
            case R.id.ticket_bind:
                id = SPUtils.get(getApplicationContext(), "Id", "").toString();
                ticketno = ticketNo.getText().toString().trim();
                if ("".equals(ticketno)) {
                    SnackbarManager.show(Snackbar.with(this).text("请输入正确的票号").duration(1000));
                } else {
                    mPresenter.saveTicketNo(id, ticketno);
                }
                break;
            case R.id.ticket_check:
                startActivity(new Intent(this, TicketDetailActivity.class));
                break;
            default:
                break;
        }
    }

    @Override
    public void showTicketState(String s) {
        TicketBindStateInfo ticketBindStateInfo = JSON.parseObject(s, TicketBindStateInfo.class);
        switch (ticketBindStateInfo.getCode()) {
            case "1":
                SnackbarManager.show(Snackbar.with(this).text("绑定失败，票号已被绑定").duration(1000));
                break;
            case "2":
                SnackbarManager.show(Snackbar.with(this).text("票号不正确").duration(1000));
                break;
            case "3":
                SnackbarManager.show(Snackbar.with(this).text("绑定失败，请重新绑定").duration(1000));
                break;
            case "4":
                SPUtils.put(getApplicationContext(), "ticketFlag", ticketno);
                SnackbarManager.show(Snackbar.with(this).text("绑定成功，大奖等你拿").duration(2000));
                Observable.timer(2, TimeUnit.SECONDS)
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(aLong -> {
                            ActivityController.getInstance().removeActivity(TicketActivity.this);
                            finish();
                        });
                break;
            case "5":
                SnackbarManager.show(Snackbar.with(this).text("您已绑定票号").duration(1000));
                break;
            default:
                showError();
                break;
        }
    }

    @Override
    public void setPresenter(TicketContract.Presenter presenter) {
        mPresenter = checkNotNull(presenter);
    }

    @Override
    protected void onResume() {
        super.onResume();
        mPresenter.subscribe();
    }

    @Override
    protected void onPause() {
        super.onPause();
        mPresenter.unsubscribe();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mPresenter.unsubscribe();
    }
}
